package com.banquetmanagementsystem.controller;

import com.banquetmanagementsystem.common.constant.TokenClaimsConstant;
import com.banquetmanagementsystem.common.context.BaseContext;
import com.banquetmanagementsystem.common.response.Result;
import com.banquetmanagementsystem.service.NewTokenService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * token续期
 *
 * @author 86176
 */
@RestController
public class NewTokenController {
    @Resource
    private NewTokenService newTokenService;

    @GetMapping("/newToken")
    public Result<?> newToken() {
        System.out.println("aa");
        Integer userId = BaseContext.getThreadLocalId();
        String username = BaseContext.getThreadLocalName();
        Boolean isWeeklyDurationEnabled = BaseContext.getThreadLocalIsWeeklyDurationEnabled();
        BaseContext.removeThreadLocalId();
        BaseContext.removeThreadLocalName();
        Map<String, String> claims = new HashMap<>(2);
        claims.put(TokenClaimsConstant.USER_ID, userId.toString());
        claims.put(TokenClaimsConstant.USERNAME, username);
        claims.put(TokenClaimsConstant.IS_WEEKLY_DURATION_ENABLED, isWeeklyDurationEnabled.toString());
        Map<String, String> tokenMap = newTokenService.newToken(claims, userId);
        return Result.success(tokenMap, "操作成功");
    }
}
